package com.example.mail;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import com.example.MessageCodes;
import com.example.popup.Alert;
import core.communication.ServerConnectorFacade;
import core.exception.BaseException;
import core.exception.LoadContextException;
import core.exception.LoadMailException;
import core.exception.SendMailException;
import core.model.Game;
import core.model.mail.Context;
import core.util.Logger;

import java.util.Collections;
import java.util.List;


public class MailManager {

    private Game game;
    private ServerConnectorFacade facade;

    private Context context;
    private List<String> races;
    private BaseException exception;

    private Activity activity;
    private ProgressDialog progressDialog;
    private Handler activityHandler;

    public MailManager(Activity activity, Game game, Handler handler) {
        this.activity = activity;
        this.game = game;
        this.facade = new ServerConnectorFacade(game);
        this.activityHandler = handler;
    }

    public void loadRaces() {
        progressDialog = ProgressDialog.show(this.activity, null, "Loading races...", true, false);
        exception = null;
        new Thread(new Runnable() {
            @Override
            public void run() {

                Logger.debug("MailManager", "Thread for loading races was created.");
                try {
                    races = facade.loadRaces(game.getParty());
                } catch (LoadMailException e) {
                    exception = e;
                }

                progressDialog.dismiss();
                loadRacesHandler.sendEmptyMessage(MessageCodes.Mail.LOADING_RACES_COMPLETE);
            }
        }).start();
    }

    public Handler loadRacesHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            progressDialog.dismiss();

            if (exception != null) {
                Logger.debug("MailManager", "Exception occurs during races loading.");
                Logger.error(exception);
                Alert.error(activity, exception);
                activityHandler.sendEmptyMessage(MessageCodes.EXCEPTION);
                return;
            }

            Logger.debug("MailManager", "Race loading complete without any problems.");
            activityHandler.sendEmptyMessage(MessageCodes.Mail.LOADING_RACES_COMPLETE);
        }
    };

    public void loadMessages() {
        progressDialog = ProgressDialog.show(this.activity, null, "Loading mail...", true, false);
        exception = null;
        new Thread(new Runnable() {
            @Override
            public void run() {

                Logger.debug("MailManager", "Thread for loading mail was created.");
                try {
                    context = loadMessageImpl();
                } catch (LoadContextException e) {
                    exception = e;
                } catch (LoadMailException e) {
                    exception = e;
                }

                progressDialog.dismiss();
                loadMessageshandler.sendEmptyMessage(MessageCodes.Mail.LOADING_MESSAGES_COMPLETE);
            }
        }).start();
    }

    protected Context loadMessageImpl() throws LoadContextException, LoadMailException {
        Context context = facade.loadMessages();

        Collections.sort(context.getPrivates());
        Collections.sort(context.getBroadcasts());
        return context;
    }

    public Handler loadMessageshandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            progressDialog.dismiss();

            if (exception != null) {
                Logger.debug("MailManager", "Exception occurs during mail loading.");
                Logger.error(exception);
                Alert.error(activity, exception);
                activityHandler.sendEmptyMessage(MessageCodes.EXCEPTION);
                return;
            }

            Logger.debug("MailManager", "Mail loading complete without any problems.");
            activityHandler.sendEmptyMessage(MessageCodes.Mail.LOADING_MESSAGES_COMPLETE);
        }
    };

    public Handler sendMessageshandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            progressDialog.dismiss();

            if (exception != null) {
                Logger.debug("MailManager", "Exception occurs during mail sending.");
                Logger.error(exception);
                Alert.error(activity, exception);
                activityHandler.sendEmptyMessage(MessageCodes.EXCEPTION);
                return;
            }

            Logger.debug("MailManager", "Mail sending complete without any problems.");
            activityHandler.sendEmptyMessage(MessageCodes.Mail.SENDING_COMPLETE);
        }
    };


    public Context getContext() {
        return context;
    }

    public List<String> getRaces() {
        return races;
    }

    public void sendMessage(final String to, final String text) {
        progressDialog = ProgressDialog.show(this.activity, null, "Sending mail...", true, false);
        exception = null;
        new Thread(new Runnable() {
            @Override
            public void run() {

                Logger.debug("MailManager", "Thread for sending mail was created.");

                try {
                    sendMessageImpl(to, text);
                } catch (SendMailException e) {
                    exception = e;
                }

                progressDialog.dismiss();
                sendMessageshandler.sendEmptyMessage(MessageCodes.Mail.SENDING_COMPLETE);
            }
        }).start();
    }

    private void sendMessageImpl(String to, String text) throws SendMailException {
        facade.sendMessage(to, text);
    }

    private void sendMessageImpl(String text) throws SendMailException {
        facade.sendBroadcast(text);
    }


    public void sendBroadcast(final String text) {
        progressDialog = ProgressDialog.show(this.activity, null, "Sending broadcast...", true, false);
        exception = null;
        new Thread(new Runnable() {
            @Override
            public void run() {

                Logger.debug("MailManager", "Thread for sending broadcast was created.");

                try {
                    sendMessageImpl(text);
                } catch (SendMailException e) {
                    exception = e;
                }

                progressDialog.dismiss();
                sendMessageshandler.sendEmptyMessage(MessageCodes.Mail.SENDING_COMPLETE);
            }
        }).start();
    }
}
